<?php
/**
 * RichCore Forms
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Form
 * @package    Form_Person
 * @copyright  Copyright (c) 2005-2010 RichCongress. (http://www.richcongress.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Edit.php 20096 2010-09-23 02:05:09Z benjamin $
 */



class Form_Person_Edit extends ZendX_JQuery_Form {
	
	function init() {
		
		// construct form element
		parent::__construct($options);
		$data = $options['data'];
		$this->setAction($options['action'])
		     ->setMethod('post')
		     ->setAttrib('class', 'form')
		     ->setAttrib('name', 'person-form')
		     ->setAttrib('id', 'person-form')
		     ->setEnctype(Zend_Form::ENCTYPE_MULTIPART)
		     ->removeDecorator('tag');  

		// add person name
		$firstname = new Zend_Form_Element_Text('firstname');
		$firstname->setAutoInsertNotEmptyValidator(true)
			 ->setRequired(true)
	         ->setValue($data['firstname'])
	         ->addFilter('StripTags')
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->addValidator('NotEmpty')
	         ->setLabel(('PERSON FIRSTNAME'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 )); 
		
		// add person lastname
		$lastname = new Zend_Form_Element_Text('lastname');
		$lastname->setAutoInsertNotEmptyValidator(true)
			 ->setRequired(true)
	         ->setValue($data['lastname'])
	         ->addFilter('StripTags')
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->addValidator('NotEmpty')
	         ->setLabel(('PERSON LASTNAME'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 )); 

		// add person email
		$email = new Zend_Form_Element_Text('email');
		$email->setAutoInsertNotEmptyValidator(true)
			 ->setValue($data['email'])
	         ->setRequired(true)
	         ->addFilter('StripTags')
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->addValidator('NotEmpty')
	         ->setLabel(('PERSON EMAIL'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 )); 	 

		// add person address
		$address = new Zend_Form_Element_Textarea('address');
		$address ->setValue($data['address'])
	         	 ->addFilter('StripTags')
		         ->addFilter('StringTrim')
		         ->setAttrib('class', 'textfield')
		         ->setLabel(('PERSON ADDRESS'))
		         ->setDecorators(array(
						    'ViewHelper',
						    'Description',
						    'Errors',
						    array('HtmlTag', array('tag' => 'span')),
						    array('Label', array('tag' => 'label')),
				 )); 

		// add person zipcode
		$zipcode = new Zend_Form_Element_Text('zipcode');
		$zipcode->addFilter('StripTags')
	         ->setValue($data['zipcode'])
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->setAttrib('maxlength', '5')
	         ->setLabel(('PERSON ZIPCODE'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 )); 		 
		
		// add person city
		$city = new Zend_Form_Element_Text('city');
		$city->setValue($data['city'])
	         ->addFilter('StripTags')
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->setLabel(('PERSON CITY'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 ));	

		// add person phone
		$phone = new Zend_Form_Element_Text('telephone');
		$phone->setValue($data['telephone'])
	         ->addFilter('StripTags')
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->setAttrib('maxlength', '14')
	         ->setLabel(('PERSON PHONE'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 ));

		// add person fax
		$fax = new Zend_Form_Element_Text('fax');
		$fax ->setValue($data['fax'])
	         ->addFilter('StripTags')
	         ->addFilter('StringTrim')
	         ->setAttrib('class', 'textfield')
	         ->setAttrib('maxlength', '14')
	         ->setLabel(('PERSON FAX'))
	         ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 ));
			 
		//add photo
		$extensions = array('jpg');
		$photo = new Zend_Form_Element_File('photo');
		$photo->setAutoInsertNotEmptyValidator(true)
					 ->setLabel(('PERSON PHOTO'))
			         ->setDestination('../temp/uploads/')
			         ->setAttrib('class', 'filefields')
			         ->setAttrib('accept', join('|', $extensions))
			         ->addValidator('Extension', false, join(',', $extensions))
			         ->setDecorators(array(
						'File',
						'Description',
						'Errors',
						array(array('data'=>'HtmlTag'), array('tag' => 'td')),
						array('Label', array('tag' => 'td')),
						array(array('row'=>'HtmlTag'),array('tag'=>'tr')) 
					));
		
		//remove photo
		if (is_file('../temp/thumbs/persons/'.$data['id'].'.jpg')) {
			$remphoto = new Zend_Form_Element_Checkbox('rem-photo');
			$remphoto->addFilter('StripTags')
	        	 	 ->addFilter('StringTrim')
	        		 ->setLabel(('PERSON REMOVE PHOTO'))
	         		 ->setDecorators(array(
					    'ViewHelper',
					    'Description',
					    'Errors',
					    array('HtmlTag', array('tag' => 'span')),
					    array('Label', array('tag' => 'label')),
			 ));
		}


		//structures list
		$structures = new Model_Database_Structure();
		
		$select = $structures
					->getAdapter()
					->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
					->from(array('s' => 'structures'))
					->order('name ASC');
					
		//make query
		$ssquery = $structures->getAdapter()->select()->from(array('ss' => 'structures_structures'), array('id' => 'ss.id_structure'))->__toString();
		$parents = $select->where('s.id not in (?)', new Zend_Db_Expr($ssquery))->query()->fetchAll();
		$select->reset( Zend_Db_Select::WHERE );
		
		$pattern = $select
					->join(array('ss' => 'structures_structures'), 'ss.id_structure = s.id', array('parent' => 'ss.id_structure_parent'))
					->where('ss.id_structure_parent=?', '%s')->__toString();
		
		$tree = new RichCongress_Utility_Tree($structures);
		$tree->setPointer('id');
		$tree->setDesc('name');
		$tree->setDepth(1000);
		$tree->setQueryDepth($pattern);
		$tree->setList($parents);
		$list = $tree->toFlat();	
				
		//add structures
		$structures = new Zend_Form_Element_Multiselect('id_structure');
	    $structures->setAttrib('multiple', 'multiple')
					 ->setRegisterInArrayValidator(false)
					 ->addMultiOptions( $list )
					 //->setValue(explode(';', $data['id_structure']))
				     ->setLabel(('PERSON STRUCTURE'))
			         ->setAttrib('class', 'filefields multiselect');

		$this->addElement($firstname)
			 ->addElement($lastname)
			 ->addElement($email)
			 ->addElement($photo);
		if (is_file('../temp/thumbs/persons/'.$data['id'].'.jpg')) {
			  	$this->addElement($remphoto);
		}

		$this->addElement($structures)
			 ->addElement($address)
			 ->addElement($zipcode)
			 ->addElement($city)
			 ->addElement($phone)
			 ->addElement($fax);
	}
}